package cn.work.service;

import cn.work.dao.reservaDao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class BatchOperationService {

    private final reservaDao reservaDao = new reservaDao();

    public Map<String, Object> processBatchOperation(List<Integer> ids, String action) {
        Map<String, Object> result = new HashMap<>();
        List<Integer> successIds = new ArrayList<>();
        List<Integer> failedIds = new ArrayList<>();

        // 定义一个映射，将英文的 action 转换为数据库中的中文状态
        String state = getStateFromAction(action);

        try {
            switch (action) {
                case "complete":
                case "cancel":
                    // 使用转换后的中文状态进行批量更新
                    reservaDao.batchUpdateState(ids, state, successIds, failedIds);
                    break;
                case "delete":
                    reservaDao.batchDeleteReservation(ids, successIds, failedIds);
                    break;
                default:
                    throw new IllegalArgumentException("未知的操作类型: " + action);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            failedIds.addAll(ids);  // 若出现异常，则所有操作失败
        }

        result.put("success", successIds);
        result.put("failed", failedIds);
        return result;
    }

    // 辅助方法：根据英文的 action 获取对应的中文状态
    private String getStateFromAction(String action) {
        switch (action) {
            case "complete":
                return "已完成";
            case "cancel":
                return "已取消";
                case "delete":
                    return "delete";
            default:
                throw new IllegalArgumentException("未知的操作类型: " + action);
        }
    }

}
